﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMukti.Business;
using VMukti.Business.CommonDataContracts;
using VMuktiAPI;

namespace VMuktiGrid
{
    /// <summary>
    /// Interaction logic for ctlVMuktiGrid.xaml
    /// </summary>
    public partial class ctlVMuktiGrid : UserControl
    {
        public ctlVMuktiGrid()
        {
            try
            {
                InitializeComponent();

                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap)
                {
                    ClsModuleCollection objCMC = ClsModuleCollection.GetNonAuthenticatedMod();
                    for (int PCnt = 0; PCnt < objCMC.Count; PCnt++)
                    {
                        ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(objCMC[PCnt].ModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);
                        int[] arrPermissionValue = new int[objCPC.Count];

                        for (int percount = 0; percount < objCPC.Count; percount++)
                        {
                            arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                        }
                        objGrid.AddControl(objCMC[PCnt].ModuleId, objCMC[PCnt].ModuleTitle, "False", null, arrPermissionValue, false, "fromDatabase");
                    }
                }
                else if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                {
                    if (VMukti.Business.clsDataBaseChannel.OpenDataBaseClient())
                    {
                        ClsModuleCollection objCMC = new ClsModuleCollection();
                        //ClsModuleCollection objCMC = ClsModuleCollection.GetNonAuthenticatedMod();
                        //System.Data.SqlClient.SqlParameter sp = new System.Data.SqlClient.SqlParameter();
                        objCMC.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName").dsInfo);

                        for (int PCnt = 0; PCnt < objCMC.Count; PCnt++)
                        {
                            ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(objCMC[PCnt].ModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);

                            int[] arrPermissionValue = new int[objCPC.Count];

                            for (int percount = 0; percount < objCPC.Count; percount++)
                            {
                                arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                            }
                            objGrid.AddControl(objCMC[PCnt].ModuleId, objCMC[PCnt].ModuleTitle, "False", null, arrPermissionValue, false, "fromDatabase");
                        }
                    }
                }

                ///////New code is as above old but running with grid without http is as bellow..
                //ClsModuleCollection objCMC = ClsModuleCollection.GetNonAuthenticatedMod();
                //for (int PCnt = 0; PCnt < objCMC.Count; PCnt++)
                //{
                //    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(objCMC[PCnt].ModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);

                //    int[] arrPermissionValue = new int[objCPC.Count];

                //    for (int percount = 0; percount < objCPC.Count; percount++)
                //    {
                //        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                //    }
                //    objGrid.AddControl(objCMC[PCnt].ModuleId, objCMC[PCnt].ModuleTitle, "False", null, arrPermissionValue, false, "fromDatabase");
                //}
                VMuktiAPI.VMuktiHelper.RegisterEvent("SignOut").VMuktiEvent += new VMuktiEvents.VMuktiEventHandler(ctlVMuktiGrid_VMuktiEvent);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlVMuktiGrid()--:--cctlVMuktiGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }        
        }

        void ctlVMuktiGrid_VMuktiEvent(object sender, VMuktiEventArgs e)
        {
            try
            {
                pageControl.Items.Clear();
                ((Button)pageControl.Template.FindName("PART_NewTabButton", pageControl)).RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

                ((VMuktiGrid.CustomMenu.ctlPgTabHeader)((VMuktiGrid.ctlPage.TabItem)pageControl.Items[0]).Header).Title = "Default Page";
                ((VMuktiGrid.CustomMenu.ctlPgTabHeader)((VMuktiGrid.ctlTab.TabItem)((VMuktiGrid.ctlTab.TabControl)((VMuktiGrid.ctlPage.TabItem)pageControl.Items[0]).Content).Items[0]).Header).Title = "Default Tab";

                ClsModuleCollection objCMC = ClsModuleCollection.GetNonAuthenticatedMod();
                for (int PCnt = 0; PCnt < objCMC.Count; PCnt++)
                {
                    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(objCMC[PCnt].ModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);

                    int[] arrPermissionValue = new int[objCPC.Count];

                    for (int percount = 0; percount < objCPC.Count; percount++)
                    {
                        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                    }
                    ((VMuktiGrid.CustomGrid.ctlGrid)((VMuktiGrid.ctlTab.TabItem)((VMuktiGrid.ctlTab.TabControl)((VMuktiGrid.ctlPage.TabItem)pageControl.Items[0]).Content).Items[0]).Content).AddControl(objCMC[PCnt].ModuleId, objCMC[PCnt].ModuleTitle, "False", null, arrPermissionValue, false, "fromDatabase");
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlVMuktiGrid_VMuktiEvent()--:--cctlVMuktiGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            } 
        }

        public void LoadPage(int pageID)
        {
            try
            {
                pageControl.LoadPage(pageID);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LoadPage()--:--cctlVMuktiGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void LoadMeetingPage(clsPageInfo objPageInfo)
        {
            try
            {
                int pCnt = 0;
                for (pCnt = 0; pCnt < pageControl.Items.Count; pCnt++)
                {
                    if (((VMuktiGrid.ctlPage.TabItem)pageControl.Items[pCnt]).OwnerID == objPageInfo.intOwnerID && ((VMuktiGrid.ctlPage.TabItem)pageControl.Items[pCnt]).OwnerPageIndex == objPageInfo.intOwnerPageIndex)
                    {
                        pageControl.LoadMeetingPage(objPageInfo, pCnt);
                        break;
                    }
                }
                if (pCnt == pageControl.Items.Count)
                {
                    pageControl.LoadNewMeetingPage(objPageInfo);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LoadMeetingPage()--:--cctlVMuktiGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }
    }
}
